﻿using System;
using Microsoft.SharePoint;

namespace SPFluid.Test
{
    public abstract class SPTestBase
    {
        protected abstract string SiteUrl { get; }

        protected abstract string WebUrl { get; }

        public void UsingSite(Action<SPSite> action)
        {
            using (var site = new SPSite(this.SiteUrl))
            {
                action(site);
            }
        }

        public T UsingSite<T>(Func<SPSite, T> func)
        {
            using (var site = new SPSite(this.SiteUrl))
            {
                return func(site);
            }
        }

        public void UsingWeb(Action<SPWeb> action)
        {
            this.UsingSite(
                site =>
                {
                    using (var web = site.OpenWeb(this.WebUrl))
                    {
                        action(web);
                    }
                });
        }

        public T UsingWeb<T>(Func<SPWeb, T> func)
        {
            return this.UsingSite(
                site =>
                {
                    using (var web = site.OpenWeb(this.WebUrl))
                    {
                        return func(web);
                    }
                });
        }
    }
}